﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Timers;

namespace BraveFoxDriver
{
    class Program
    {

        static void Main(string[] args)
        {

            int IPport = 25000;
            bool controlled = false;

            iRobotCreate robot = new iRobotCreate();

            Console.Out.WriteLine("Enter the COM Port Number of the robot: ");
            String comport = Console.In.ReadLine();

            while (!robot.Connect("COM"+comport))
            {
                Console.Out.WriteLine("Bad Comport, Try Again");
                Console.Out.WriteLine("Enter the COM Port Number of the robot: ");
                comport = Console.In.ReadLine();
            }

            Console.Out.WriteLine("Enter the password you wish to assign to the robot: ");
            String password = Console.In.ReadLine();

            UdpClient socket = new UdpClient(IPport);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);

            socket.AllowNatTraversal(true);

            WebClient webClient = new WebClient(); 
            string IP = webClient.DownloadString("http://myip.ozymo.com/"); 
            webClient.Dispose();

            Console.Out.WriteLine("External IP: " + IP);
            Console.Out.WriteLine("Waiting for Driver...");

            robot.StartInFullMode();

            while (true)
            {
                Byte[] receivedBytes = socket.Receive(ref endPoint);
                string receivedString = Encoding.ASCII.GetString(receivedBytes);
                Console.Out.WriteLine("Received: " + receivedString);
                string[] parts = receivedString.Split(' ');

                if (parts.Length == 2)
                {
                    if (parts[0].CompareTo("control") == 0)
                    {
                        if (!controlled)
                        {
                            Console.Out.WriteLine("received connection request");
                            if (parts[1].CompareTo(password) == 0)
                            {
                                controlled = true;
                                Console.Out.WriteLine("remote control granted!");

                                Byte[] sendBytes = Encoding.ASCII.GetBytes("control ok");
                                socket.Send(sendBytes, sendBytes.Length, endPoint.Address.ToString(), endPoint.Port);
                            }
                            else
                            {
                                Console.Out.WriteLine("password wrong!");

                                Byte[] sendBytes = Encoding.ASCII.GetBytes("control no");
                                socket.Send(sendBytes, sendBytes.Length, endPoint.Address.ToString(), endPoint.Port);
                            }
                        }
                        else
                        {
                            Console.Out.WriteLine("Already controlled, sorry brah");
                        }

                    }
                    else
                    {
                        int left = int.Parse(parts[0]);
                        int right = int.Parse(parts[1]);

                        robot.DriveDirect(left, right);

                    }
                }
                else if(parts[0].CompareTo("disconnect") == 0)
                {
                    controlled = false;
                    Console.Out.WriteLine("Client Disconneted Voluntarily");
                }

            }
        }
    }
}
